package org.vellos.halide.interpret;

import org.vellos.halide.parse.ExpressionTree;
import org.vellos.halide.parse.Parser;
import org.vellos.halide.vm.VirtualMachine;

public class Interpreter {

  private final VirtualMachine virtualMachine;

  public Interpreter(VirtualMachine virtualMachine) {
    this.virtualMachine = virtualMachine;
  }

  public String evaluate(String readLine) throws Exception {
    ExpressionTree tree = Parser.parseExpressionToTree(readLine);

    while (tree.getRoot() != tree.getFirstLeaf()) {
      tree.replaceLeaf(virtualMachine.evaluateNode(tree.getFirstLeaf()));
    }

    return virtualMachine.evaluateNode(tree.getRoot());
  }
}
